<template>
	<div>
		<el-tabs v-model="activeName" @tab-click="handleTabChange">
			<el-tab-pane v-for="(item, index) in tabsList" :key="index" :label="item.label" :name="item.value"></el-tab-pane>

			<div class="content-card">
				<html-label :title="activeLabelTitle">
					<div v-if="!(nowRowData.dataStatus && nowRowData.dataStatus !== '0') && ['BaseDetail', 'Extend', 'ObjectOther'].includes(activeName)">
						<el-button
							v-auth="[nowRowData.isOperate == 1, baseConfig.auth.edit]"
							type="primary"
							plain
							@click="handleOperate(activeName == 'ObjectOther' ? 'rechange' : 'rechange')"
						>
							信息变更
						</el-button>
					</div>
				</html-label>
				<component
					v-if="!loading"
					class="pd10"
					:is="whickComm[activeName]"
					@refresh="handleRefresh"
					:nowRowData="nowRowData"
					:actionBtn="false"
					:searchConfig="false"
					:key="activeName"
					:chainViewInfo="nowRowData"
					:isBlockchain="isBlockchain"
					:isWithAuth="0"
					:soleKey="`dpServiceObject-${nowRowData.idCard}`"
				></component>
			</div>
		</el-tabs>
	</div>
</template>

<script setup lang="ts">
import Extend from '@/views/datacenter/basicData/oldman/components/Archives/Extend/index.vue'
import BaseDetail from '@/views/datacenter/basicData/oldman/components/Archives/BaseDetail/index.vue'
import ObjectOther from '@/views/datacenter/basicData/oldman/components/Archives/ObjectOther/index.vue'
import BlockchainInfo from '@/views/datacenter/basicData/oldman/components/Archives/BlockchainInfo/index.vue'
import apiMod from '@/apiMod'
import { baseConfig } from '@/views/datacenter/basicData/oldman/config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
import bus from '@/utils/eventbus'
import { getAreaObj } from '@/utils/util'

const { setTempParams, setMoreSearch, setLoading, setGloabnowRowData } = useCommonStore()
const { onDelete, onMessageChange } = useOperate(1)

const whickComm = {
	BaseDetail,
	Extend,
	ObjectOther,
	BlockchainInfo,
}
type Props = {
	nowRowData?: any
	isBlockchain?: boolean
}
const props = withDefaults(defineProps<Props>(), {
	isBlockchain: false,
})
const emit = defineEmits(['refresh', 'rechange'])



const activeLabelTitle = ref('基础信息')
const loading = ref(false)
const tabsList: any = [
	{ label: '基础信息', value: 'BaseDetail' },
	{ label: '扩展信息', value: 'Extend' },
	{ label: '其他信息', value: 'ObjectOther' },
	{ label: '上链信息', value: 'BlockchainInfo' },
]
const activeName = ref('BaseDetail')
let nowData = ref({})
provide('nowRowData', nowData)
const getInitData = () => {
	// 如果是区块链查看页面的话
	if (props.isBlockchain) {
    loading.value = true 
    setLoading(true)
		nowData.value = {
			dpServiceObjectContacts: props.nowRowData.bcServiceObjectContact ? JSON.parse(props.nowRowData.bcServiceObjectContact) : [],
			dpServiceObjectExtends:{
        ...props.nowRowData
      },
			dpServiceObjectMaterials: props.nowRowData.bcServiceObjectMaterial ? JSON.parse(props.nowRowData.bcServiceObjectMaterial) : [],
			dpServiceObjectOthers: {
        ...props.nowRowData
      },
			dpServiceObjects: {
        ...props.nowRowData
      },
		}
    setGloabnowRowData(nowData.value)
    setTimeout(() => {
      loading.value = false
      setLoading(false)
    }, 100);
	} 
}

const openEdit = () => {
	onMessageChange().then(() => {
		const activeStr = {
			BaseDetail: 1,
			Extend: 2,
			ObjectOther: 3,
		}
		setTempParams({
			idCard: props.nowRowData.idCard,
			active: activeStr[activeName.value] || 1,
		})
		emit('rechange', 'Rechange', props.nowRowData, '信息变更数据')
	})
}

const handleOperate = (type) => {
	if (type == 'delete') {
		onDelete(props.nowRowData[baseConfig.rowKey], baseConfig.api.delete, props.nowRowData.name).then(() => {
			emit('refresh')
		})
		return
	} else if (type == 'rechange') {
		openEdit()
	} else {
		emit('rechange', 'edit', props.nowRowData, '信息编辑数据')
	}
}

const handleRefresh = () => {
	emit('refresh')
}
const handleTabChange = (tab) => {
	activeLabelTitle.value = tab.props.label
	setMoreSearch({
		params: {
			idCard: props.nowRowData.idCard,
			creditCode: props.nowRowData.creditCode,
		},
	})
}

bus.on('menu-operate', (res) => {
	handleOperate(res.operate)
})

onMounted(() => {
	getInitData()
})
onUnmounted(() => {
	setMoreSearch({})
})
</script>

<style scoped></style>